﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DWEIP.Common;
using DataWin.Core.ORM;
using DataWin.Core.Model;
using DataWin.Core.Util;
using DWEIP.Models.OA;
using DWEIP.Locale;
using DWEIP.BLL.Core;

namespace DWEIP.BLL.OA
{
    public partial class OA_BltBLL : BasicBLL
    {
        #region DAL
        public nblt001Model DAL_GetById(int Pk_Id)
        {
            Table _nblt001 = new Table("nblt001");
            _nblt001.SQL.Value = @"
SELECT * FROM nblt001 Where pk_id = @id";
            _nblt001.Params.Clear();
            _nblt001.Params.Add("id", new Data(Pk_Id));

            BLLSession.Load(_nblt001);

            var msgModelList = (from r in _nblt001
                                select new nblt001Model
                                {
                                    recid = r["recid"].AsString,
                                    pk_id = r["pk_id"].AsInt,
                                    function_id = r["function_id"].AsString,
                                    folder_id = r["folder_id"].AsInt,
                                    create_user = r["create_user"].AsString,
                                    create_date = r["create_date"].AsDateTime,
                                    modify_user = r["modify_user"].AsString,
                                    modify_date = r["modify_date"].AsDateTime,
                                    last_user = r["last_user"].AsString,
                                    last_date = r["last_date"].AsDateTime,
                                    doc_status = r["doc_status"].AsString,
                                    ref_browse = r["ref_browse"].AsInt,
                                    title = r["title"].AsString,
                                    content = r["content"].AsString,
                                    cont_format = r["cont_format"].AsInt,
                                    cont_text = r["cont_text"].AsString,
                                    publish_date = r["publish_date"].AsString,
                                    priority = r["priority"].AsInt,
                                    blt_type = r["blt_type"].AsString,
                                }).ToList();
            return msgModelList[0];
        }

        public bool DAL_Create(nblt001Model model)
        {
            string sqltxt = @"INSERT INTO nblt001 
recid,
--pk_id,
function_id,
folder_id,
doc_status,
title,
content,
cont_format,
cont_text,
publish_date,
priority,
blt_type,
create_user,
create_date)
VALUES('{0}', '{1}', {2}, '{3}', '{4}', '{5}', {6}, '{7}', '{8}', {9}, '{10}', '{11}', '{12}')";

            Table _nblt001 = new Table("nblt001");
            _nblt001.SQL.Value = string.Format(sqltxt,
                model.recid,
                //model.pk_id,
                model.function_id,
                model.folder_id,
                model.doc_status,
                model.title,
                model.content,
                model.cont_format,
                model.cont_text,
                model.publish_date,
                model.priority,
                model.blt_type,
                model.create_user,
                model.create_date
                );

            int result = BLLSession.Execute(_nblt001, System.Data.CommandType.Text);
            return result > 0;
        }

        public bool DAL_Edit(nblt001Model model)
        {
            string sqltxt = @"UPDATE nblt001 SET 
function_id='{1}',
folder_id={2},
create_user='{3}',
create_date='{4}',
modify_user='{5}',
modify_date='{6}',
last_user='{7}',
last_date='{8}',
doc_status='{9}',
ref_browse={10},
title='{11}',
content='{12}',
cont_format={13},
cont_text='{14}',
publish_date='{15}',
priority={16},
blt_type='{17}'
WHERE pk_id={0}";

            Table _nblt001 = new Table("nblt001");
            _nblt001.SQL.Value = string.Format(sqltxt,
model.pk_id,
model.function_id,
model.folder_id,
model.create_user,
model.create_date,
model.modify_user,
model.modify_date,
model.last_user,
model.last_date,
model.doc_status,
model.ref_browse,
model.title,
model.content,
model.cont_format,
model.cont_text,
model.publish_date,
model.priority,
model.blt_type
                );
            int result = BLLSession.Execute(_nblt001, System.Data.CommandType.Text);
            return result > 0;
        }

        public int DAL_Delete(int Pk_Id)
        {
            Table _nblt001 = new Table("nblt001");
            _nblt001.SQL.Value = @"
DELETE nblt001 Where pk_id=@id";
            _nblt001.Params.Clear();
            _nblt001.Params.Add("id", new Data(Pk_Id));

            int result = BLLSession.Execute(_nblt001, System.Data.CommandType.Text);
            return result;
        }

        #endregion DAL

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="pager">JQgrid分页</param>
        /// <param name="queryStr">搜索条件</param>
        /// <returns>列表</returns>
        public List<nblt001Model> GetList(ref GridPager pager, string queryStr, string cid, bool isManage, string userId, int check)
        {
            if (BLLSession == null)
            {
                return null;
            }

            BLLSession.BeginTransaction();
            Table _nblt001 = new Table("nblt001");
            _nblt001.SQL.Value = @"
SELECT nobj002.title folder_title,nblt001.*,
DV.master_view as master_view,DV.daily_report_view as daily_report_view,DV.dialogue_view as dialogue_view,DV.attach_view as attach_view,
CASE WHEN DV.master_view=0 OR DV.daily_report_view=0 OR DV.dialogue_view=0 OR DV.attach_view=0 THEN 0 ELSE 1 END AS view_state,
CreateUserAbbr=(SELECT ae03 FROM tam05 WHERE ae01=nblt001.create_user),hp_keep,ISNULL(nlog001.click_count,0) as click_count,nlog001.is_view,nblt001.publish_date 
FROM nblt001 LEFT JOIN nlog001 ON nblt001.recid=nlog001.mst_recid AND click_user=@curruser
LEFT JOIN nobj002 ON nobj002.function_id='S1' and nobj002.folder_id=nblt001.folder_id
OUTER APPLY dbo.afv_OA_DataIsView(nblt001.recid,nblt001.function_id,nblt001.pk_id,@curruser) as DV  
WHERE nblt001.doc_status='' AND nblt001.publish_date<@nowdate
    AND ((isnull(nlog001.is_view,0)=0 AND nblt001.publish_date>=@startdate AND nblt001.publish_date<=@enddate) OR (nlog001.hp_keep=1))
--TODO: INIT";
            _nblt001.Params.Clear();
            _nblt001.Params.Add("curruser", new Data(userId));
            _nblt001.Params.Add("startdate", new Data("19110101"));
            _nblt001.Params.Add("enddate", new Data("29991231"));
            _nblt001.Params.Add("nowdate", new Data("29991231"));
            BLLSession.Load(_nblt001);
            BLLSession.Commit();

            List<nblt001Model> _nblt001s = new List<nblt001Model>();
            while (!_nblt001.Eof)
            {
                nblt001Model _nblt001_i = new nblt001Model();

                _nblt001_i.recid = _nblt001["recid"].AsString;
                _nblt001_i.pk_id = _nblt001["pk_id"].AsInt;
                _nblt001_i.function_id = _nblt001["function_id"].AsString;
                _nblt001_i.folder_id = _nblt001["folder_id"].AsInt;
                _nblt001_i.create_user = _nblt001["create_user"].AsString;
                _nblt001_i.create_date = _nblt001["create_date"].AsDateTime;
                _nblt001_i.modify_user = _nblt001["modify_user"].AsString;
                _nblt001_i.modify_date = _nblt001["modify_date"].AsDateTime;
                _nblt001_i.last_user = _nblt001["last_user"].AsString;
                _nblt001_i.last_date = _nblt001["last_date"].AsDateTime;
                _nblt001_i.doc_status = _nblt001["doc_status"].AsString;
                _nblt001_i.ref_browse = _nblt001["ref_browse"].AsInt;
                _nblt001_i.title = _nblt001["title"].AsString;
                _nblt001_i.content = _nblt001["content"].AsString;
                _nblt001_i.cont_format = _nblt001["cont_format"].AsInt;
                _nblt001_i.cont_text = _nblt001["cont_text"].AsString;
                _nblt001_i.publish_date = _nblt001["publish_date"].AsString;
                _nblt001_i.priority = _nblt001["priority"].AsInt;
                _nblt001_i.blt_type = _nblt001["blt_type"].AsString;

                _nblt001_i.folder_title = _nblt001["folder_title"].AsString;

                _nblt001s.Add(_nblt001_i);

                _nblt001.Next();
            }

            return _nblt001s;
        }

        public bool Create(ref ValidationErrors errors, nblt001Model model)
        {
            try
            {
                nblt001Model entity = DAL_GetById(model.pk_id);
                if (entity != null)
                {
                    errors.Add(Resource.PrimaryRepeat);
                    return false;
                }
                entity = new nblt001Model();
                entity.recid = model.recid;
                entity.pk_id = model.pk_id;
                entity.function_id = model.function_id;
                entity.folder_id = model.folder_id;
                entity.create_user = model.create_user;
                entity.create_date = model.create_date;
                entity.modify_user = model.modify_user;
                entity.modify_date = model.modify_date;
                entity.last_user = model.last_user;
                entity.last_date = model.last_date;
                entity.doc_status = model.doc_status;
                entity.ref_browse = model.ref_browse;
                entity.title = model.title;
                entity.content = model.content;
                entity.cont_format = model.cont_format;
                entity.cont_text = model.cont_text;
                entity.publish_date = model.publish_date;
                entity.priority = model.priority;
                entity.blt_type = model.blt_type;
                if (DAL_Create(entity))
                {
                    return true;
                }
                else
                {
                    errors.Add(Resource.InsertFail);
                    return false;
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

        public bool Edit(ref ValidationErrors errors, nblt001Model model)
        {
            try
            {
                nblt001Model entity = DAL_GetById(model.pk_id);
                if (entity == null)
                {
                    errors.Add(Resource.Disable);
                    return false;
                }
                entity.recid = model.recid;
                entity.pk_id = model.pk_id;
                entity.function_id = model.function_id;
                entity.folder_id = model.folder_id;
                entity.create_user = model.create_user;
                entity.create_date = model.create_date;
                entity.modify_user = model.modify_user;
                entity.modify_date = model.modify_date;
                entity.last_user = model.last_user;
                entity.last_date = model.last_date;
                entity.doc_status = model.doc_status;
                entity.ref_browse = model.ref_browse;
                entity.title = model.title;
                entity.content = model.content;
                entity.cont_format = model.cont_format;
                entity.cont_text = model.cont_text;
                entity.publish_date = model.publish_date;
                entity.priority = model.priority;
                entity.blt_type = model.blt_type;

                if (DAL_Edit(entity))
                {
                    return true;
                }
                else
                {
                    errors.Add(Resource.NoDataChange);
                    return false;
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

        public bool Delete(ref ValidationErrors errors, int id)
        {
            try
            {
                if (DAL_Delete(id) == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

    }
}
